package com.duoduo.modules.service.impl;

import com.duoduo.common.entity.AuthUser;
import com.duoduo.common.mapper.read.ReadAuthUserMapper;
import com.duoduo.common.mapper.write.WriteAuthUserMapper;
import com.duoduo.common.response.Result;
import com.duoduo.common.util.StringUtils;
import com.duoduo.modules.service.AuthUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 用户服务实现类
 * @Author:
 * @CreateTime: 2023-10-18  09:23
 * @Description:
 * @Version: 1.0
 */
@Service
public class AuthUserServiceImpl implements AuthUserService {
    @Autowired
    ReadAuthUserMapper readAuthUserMapper;
    @Autowired
    WriteAuthUserMapper writeAuthUserMapper;

    @Override
    public AuthUser queryUserById(Long id) {
        return this.readAuthUserMapper.queryById(id);
    }

    @Override
    public List<AuthUser> queryUserList(AuthUser authUser) {
        return this.readAuthUserMapper.selectAuthUserList(authUser);
    }

    @Override
    public Integer count(AuthUser authUser) {
        return (int)this.readAuthUserMapper.count(authUser);
    }

    @Override
    public AuthUser queryByMobile(String mobile) {
        return this.readAuthUserMapper.queryByMobile(mobile);
    }

    @Override
    public Result addUser(AuthUser authUser) {
        // 查询是否存才相同的手机号
        AuthUser authUser1 = queryByMobile(authUser.getMobile());
        if (StringUtils.isNotNull(authUser1)){
            return Result.builder().code("A0111").userTip("此账号已被注册！").build();
        }
        this.writeAuthUserMapper.insert(authUser);
        return Result.builder().code("000000").userTip("注册成功").build();
    }

    @Override
    public int updateUser(AuthUser authUser) {
        return this.writeAuthUserMapper.update(authUser);
    }

    @Override
    public int removeUser(Long id) {
        return this.writeAuthUserMapper.deleteById(id);
    }
}
